package interview_questions;

public class Main1_4 {
	public static void replaceSpaces(char[] str, int length) {
		int spaceCount = 0, newLength, i;
		for(i = 0; i < length; i++)
			if(str[i] == ' ') spaceCount++;
		newLength = length + spaceCount * 2;
		str[newLength] = '\0';
		for(i = length - 1; i >= 0; i--) {
			if(str[i] == ' ') {
				str[newLength - 1] = '0';
				str[newLength - 2] = '2';
				str[newLength - 3] = '%';
				newLength = newLength - 3;
			} else {
				str[newLength - 1] = str[i];
				newLength = newLength - 1;
			}
		}
	}

	public static void main(String[] args) {
		char[] s = new char[100];
		s[0] = 'a';
		s[1] = ' ';
		s[2] = 'b';
		s[3] = ' ';
		s[4] = 'c';
		replaceSpaces(s, 5);
		System.out.println(s);
	}

}
